home *** CD-ROM | disk | FTP | other *** search
- /* OffscreenGrafPort.c */
- #ifdef DOCUMENTATION
-
- Title GrafPort Handler
-
- Usage
-
- #include "OffscreenGrafPort.h"
-
- GrafPtr
- CreateOSGrafPort(rect)
- Rect rect;
-
- Create a new GrafPort big enought to hold the Rect.
- Note that its storage is not relocatable. If there
- isn't enough memory, CreateOSGrafPort will return
- NIL and the GrafPort is -- obviously -- unusable.
- If CreateOSGrafPort succeeds, the function does
- SetPort to the new GrafPtr.
-
- void
- CopyOSGrafPort(srcPort, dstPort, mask)
- GrafPtr srcPort;
- GrafPtr dstPort;
- RgnHandle mask;
-
- Copy the contents of the srcPort to the dstPort.
- The entire port is copied. Note: the picture is
- stretched to fill the dstPort. If this is
- unsuitable, just call CopyBits yourself.
-
- void
- DeleteOSGrafPort(theOSGrafPort)
- GrafPtr theOSGrafPort;
-
- Delete the GrafPort created by CreateOSGrafPort().
-
- Normal Usage:
-
- GrafPtr tempPort;
- GrafPtr oldPort;
-
- GetPort(&oldPort);
- tempPort = CreateOSGrafPort(rect);
- ... drawing commands ...
- SetPort(oldPort);
- /*
- * Show my stuff
- */
- CopyOSGrafPort(tempPort, FrontWindow(), NIL);
- DeleteOSGrafPort(tempPort);
-
- acknowledgment
-
- Taken without much change from Mac TechNote 41
-
- #endif
-
- #include "OffscreenGrafPort.h"
- #ifndef width
- #define width(r) ((r).right - (r).left)
- #define height(r) ((r).bottom - (r).top)
- #endif
-
- GrafPtr
- CreateOSGrafPort(box)
- Rect box;
- {
- GrafPtr oldPort;
- GrafPtr newPort;
- long rowBytes;
-
- /*
- * Build an "empty" port big enough for our picture.
- */
- GetPort(&oldPort);
- newPort = (GrafPtr) NewPtr(sizeof (GrafPort));
- if (newPort == NIL || MemError() != noErr)
- return (NIL);
- OpenPort(newPort);
- newPort->portRect = box;
- newPort->portBits.bounds = box;
- RectRgn(newPort->clipRgn, &box);
- RectRgn(newPort->visRgn, &box);
- rowBytes = ((width(box) + 15) >> 4) << 1;
- newPort->portBits.rowBytes = rowBytes;
- newPort->portBits.baseAddr =
- NewPtr(rowBytes * (long) height(box));
- if (newPort->portBits.baseAddr == NIL
- || MemError() != noErr) {
- SetPort(oldPort);
- ClosePort(newPort);
- DisposPtr(newPort);
- return (NIL);
- }
- /*
- * OpenPort did a SetPort to newPort
- */
- EraseRect(&box);
- return (newPort);
- }
-
- /*
- * Copy between ports. Note that the data is
- * stretched to fill the entire dstPort.
- */
- void
- CopyOSGrafPort(srcPort, dstPort, mask)
- GrafPtr srcPort;
- GrafPtr dstPort;
- RgnHandle mask;
- {
- CopyBits(
- &(*srcPort).portBits, &(*dstPort).portBits,
- &(*srcPort).portRect, &(*dstPort).portRect,
- srcCopy,
- mask
- );
- }
-
- /*
- * Dispose of the port.
- */
- void
- DeleteOSGrafPort(oldPort)
- GrafPtr oldPort;
- {
- ClosePort(oldPort);
- DisposPtr((*oldPort).portBits.baseAddr);
- DisposPtr((Ptr) oldPort);
- }
-
-
-